<?php 
namespace Admin\Controller;
use Think\Controller;
class GoodsinfoController extends Controller{
  //用传统的方法实例化模型
    /*function showdb(){
    	$model = new  \Admin\Model\GoodsinfoModel();
    	dump($model);
    }
    //用M方法实例化模型
    function showdm(){
    	$model = M('Goodsinfo');
    	dump($model);
    }
    //使用D方法实例化模型
    function showdd(){
    	$model =D('Goodsinfo'); 
    	dump($model);
    }
    function goodsadd1(){
    	$model= D('Goodsinfo');
    	$data['typeid']=3;
    	$data['pname']='宝马';
    	$data['price']=190000;
    	$data['pcount']=7;
    	$re=$model->add($data);
    	if($re){
    		echo"添加成功";
    	}else{
    		echo"添加失败";
    	}
    }
    function goodslist(){
    	$model = D('Goodsinfo');
    	//$res=$model->select('3');
    	$res=$model->find(2);
        dump($res);
        }

        function goodsupdate(){
    	$model= D('Goodsinfo');
    	$data['pid']=1;
    	$data['typeid']=8;
    	$data['pname']='日产';
    	$data['price']=400000;
    	$data['pcount']=15;
    	$re=$model->save($data);
    	if($re){
    		echo"修改成功".$re;
    	}else{
    		echo"修改失败";
    	}
    }
    function goodsdel(){
    	$model = D('Goodsinfo');
    	$res =$model->delete(2);
    	dump($res);
    }
    function showa(){
        $res1=$_GET['name'];
        $res2=I('get.name');
        var_dump($res1);
        var_dump($res2);
    }
    //函数中可以传递参数
    function showi($str=''){
     
     var_dump($str);
    }*/
function  goodsadd(){
        //当前提交我们数据的时候
        if(IS_POST){
             //实例化模型
            $model = D('Goodsinfo');
            $data['typeid']=I('post.pt');
            $data['pname']=I('post.pn');
            $data['price']=I('post.pp');
            $data['pcount']=I('post.pc');
            $res=$model->add($data);
            if($res){
                //echo"添加成功".$res;
                $this->success('添加成功'.$res,U('goodsadd'),1);
            }else{
                echo"添加失败";
            }
        }
        $this->display();
    }
    function goodslist(){
        //实例化模型
        $model =D('Goodsinfo');
        $res=$model->select();//查询数据某几条
        $this->assign('res',$res);
        $this->display();
    }
    function goodsupdate(){
        $model = D('Goodsinfo');
        //读取修改的数据
        $pid=I('get.pid');
        if(IS_GET){
            $res= $model->find($pid);
            $this->assign('res',$res);
        }
        //修改数据
        if(IS_POST){
           //dump($pid);
           $data['pid']=$pid;
           $data['typeid']=I('post.pt');
           $data['pname'] = I('post.pn');
           $data['price'] = I('post.pp');
           $data['pcount'] = I('post.pc');
           $re=$model->save($data);
           if($re){
            $this->success('修改成功'.$pid,U('goodslist'));
            //echo"修改成功".$pid;
           }else{
            $this->error('数据修改失败');
            //echo"修改失败";
           }
        }
        $this->display();
    }
    //删除数据
    function goodsdel(){
        $model = D('Goodsinfo');
        $pid=I('get.pid');
        $res=$model->delete($pid);
        if($res){
            //echo"删除成功";
            $this->success('删除成功'.$pid,U('goodslist'));
        }else{
            $this->error('删除失败',U('goodslist'));
        }
    }

    function showaa(){
        /*G('start');
        $model=D('Goodsinfo');
        //$res=$model->select();查询所有语句
        $re=$model->find();
        dump($re);
        //echo $model->getLastSql();//得到最后执行的sql语句
        echo $model->_sql();
        G('end');
        echo"<br>";
        echo G('start','end',3);//该程序执行花费的时间
        echo"<br>";
        echo G('start','end','m');//改程序在内存中占大小*/

        /*echo"面积为".circle();

        $str ='小红';

        trace($str,'str=','user');
        echo"<br>";
        echo "验证码：".yzm();*/

        /*load('@/f2');
        echo'长方形的面积为：'.xy();*/
    }
function addar(){
        $model=D('Goodsinfo');
        $model->typeid=6;
        $model->pname='奔驰';
        $model->price=500000;
        $model->pcount =50;
        $res= $model->add();
        if($res){
            echo"添加成功";
        }else{
            echo"添加失败";
        }
    }
    function update(){
        $model = D('Goodsinfo');
        //$model->pid=4;//知名修改的条件
        $model->find(4);
        $model->typeid=6;
        $model->pname='福德';
        $model->price=300000;
        $model->pcount=24;
        $res=$model->save();
        if($res){
            echo"修改OK";
        }else{
            echo"修改失败";
        }
    }
function select(){
        $model = D('Goodsinfo');
        $res = $model->find();
        $res=$model->select();
        dump($res);
    }
    function del(){
        $model = D('Goodsinfo');
        $res= $model->delete('6,10');
        dump($res);
    }
    function dell(){
        $model = D('Goodsinfo');
        $res=$model->find(4);
        $res= $model->delete();
        dump($res);
    }
    function goodscreate(){
        $model=D('Goodsinfo');
        if(IS_POST){
            //将表单中的所有表单对象的数据收集起来
            if($data=$model->create()){
                //echo"数据添加成功";
                $this->Success('添加成功',U('goodscreate'));
                //dump($data);//打印收集的数据
                $model->add();//添加数据
            }else{
                echo"数据添加失败";
                dump($model->getError());//显示错误信息
            }
        }
        $this->display();
    }
    //查询
    function goodsw(){
        $model = D('Goodsinfo');
        $res=$model->where("pname='奔驰'")->select();
        dump($res);
    }
    function goodsq(){
        $model=D('Goodsinfo');
        $res = $model->where("pname='日产'")->select();
        dump($res);
    }
    function goodsa(){
        $model = D('Goodsinfo');
        $data['pid']=18;
        $data['pname']='长安';
        $res=$model->where($data)->select();
        dump($res);
    }
    function goodss(){
        $model = D('Goodsinfo');
        $res=$model->where("pname='奔驰' or pid=18")->select();
        dump($res);
    }
    function goodsli(){
        $model=D('Goodsinfo');
        //从第几行开始查询，后面的是总数，由于返回数组，行数从零开始
        $res = $model->limit('1,5')->select();
        dump($res);
    }

    //查询分页
    function goodspage(){
        $model=D('Goodsinfo');
        //前面的是页数，后面的是记录数
        $res = $model->page('2,3')->select();
        dump($res);
    }
    //field查询方式
    function goodsf(){
        $model=D('Goodsinfo');
        $res= $model->field("pname,pid,price")->where("pid='12'")->select();
        dump($res);
    }
    function goodsorder(){
        $model=D('Goodsinfo');
        //默认是升序排序，添加desc后降序排序
        //$res = $model->order('price')->select();
        $res=$model->field('pname,count(*) as 数量,sum(price) as 总价')->
        group('pname')->select();
        dump($res);
    }

    //连贯查询

    function goodsabc(){
        $model = D('Goodsinfo');
        //先把pname,price,typeid查询，条件为typeid=0,然后按价格的降序排序
        $res = $model->field('pname,price,typeid')->where("typeid='0'")->
        order('price desc')->select();
        dump($res);
    }
    //总数
    function goodssum(){
        $model = D('Goodsinfo');
        ///$res = $model->count();
        //MAX函数
        /*$res=$model->max('price');
        $re = $model->where('$res')->select();*/
        //$re=$model->where("pid='12'")->select();
        //$re = $model->max('price');
        //$re =$model->min('price');
        //平均价格为
        //$re = $model->avg('price');
        $res = $model->sum('price');
        dump($res);
    }
    //用query方法查询数据库
    function goodsshow(){
        $model=D('Goodsinfo');
        $res=$model->query("select * from tp_goodsinfo");
        dump($res);
    }
    function goodsexe(){
        $model=D('Goodsinfo');
        //$res =$model->execute("insert into tp_goodsinfo(typeid,pname,price,pcount)
            //values(3,'东风',250000,50)");
        $res = $model->execute("update tp_goodsinfo set pname='丰田' where pid=15");
        dump($res);
    }
    //令牌验证
    function showtoken(){
        $model=D('Goodsinfo');
        if($_POST){
        //autoCheckToken:令牌验证
        if($model->autoCheckToken($_POST)){
            echo"令牌验证成功";
        }else{
            echo"令牌失败";
        }
    }
    $this->display();
 }
   //用户登录案例
   function verify(){
   $config = array(
    'fontSize'=>20,//字体大小
    'imageW'=>200, //宽度
    'imageH' =>50,//高度
    'length' =>4,//长度
        );
    $vf= new \Think\Verify($config);//调用父类
    $vf->entry();//显示验证码
   } 
 }  